﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using Microsoft.ApplicationBlocks.Data;
using System.Data.OleDb;
namespace XBYTestManagement
{
    public partial class FrmCreateSolution : KryptonForm
    {
        Product product=new Product();
        public FrmCreateSolution(Form owner)
        {
            InitializeComponent();
            this.Owner = owner;
            BindProduct();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            
            Save();
            this.Close();
            
        }

        private void kryptonComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (kryptonComboBox1.SelectedIndex == -1) return;
            DataRowView dr=(DataRowView)kryptonComboBox1.SelectedItem;
            txtName.Text = dr["ProductName"].ToString() + "测试方案";
            product.ID = (int)dr["ID"];
            product.Number = dr["Number"].ToString();
            product.ProductName = dr["ProductName"].ToString();
        }
        private void BindProduct()
        {
            DataSet ds = OleDbHelper.ExecuteDataset(OleDbHelper.connectionString, CommandType.Text, "select * from product where id not in ( select productid from solution )");
            kryptonComboBox1.DataSource = ds.Tables[0].DefaultView;
            kryptonComboBox1.DisplayMember = "Number";
            kryptonComboBox1.ValueMember = "ID";

        }
        private void Save()
        {
            String sql = "insert into solution (SolutionName,ProductID) values (@SolutionName,@ProductID)";
            OleDbParameter[] param=new OleDbParameter[2];
            param[0] = new OleDbParameter();
            param[0].ParameterName="@SolutionName";
            param[0].Value=txtName.Text.Trim();

            param[1] = new OleDbParameter();
            param[1].ParameterName="@ProductID";
            param[1].DbType=DbType.Int32;
            param[1].Value=product.ID;
            try
            {
                OleDbHelper.ExecuteNonQuery(OleDbHelper.connectionString, CommandType.Text, sql, param);
            }
            catch
            {
                throw;
            }
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            FrmProductSearch frmSearch = new FrmProductSearch();
            frmSearch.ShowDialog();
            
            DataRow dr = frmSearch.resultRow;
            if (dr == null) return;
            kryptonComboBox1.SelectedText = "";
            kryptonComboBox1.Text = "";
            kryptonComboBox1.SelectedText = dr["Number"].ToString();
            txtName.Text = dr["ProductName"].ToString() + "测试方案";
            product.ID = (int)dr["ID"];
            product.Number = dr["Number"].ToString();
            product.ProductName = dr["ProductName"].ToString();

        }
        
    }
}
